package com.xin.ruanjian.omsystem.bases;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.utils.ToastUtils;

import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by local123 on 2017/12/31.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected BaseActivity context;
    protected Toolbar mToolbar;
    protected TextView mTvCenterTitle;
    protected boolean isDestroy;
    protected SweetAlertDialog waitDialog;
    protected final String TAG = "BaseActivity";

    private boolean dialogIsCancelable = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /** 强制竖屏 */
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initContentView(savedInstanceState);
        ButterKnife.bind(this);
        setStatusBarFullTransparent();
        setFitSystemWindow(true);
        context = this;
        isDestroy = false;
        initData();
        initView();
        mToolbar = findViewById(R.id.toolbar);
        mTvCenterTitle = findViewById(R.id.tv_centerTitle);
        if (null != mToolbar) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            initTitle();
        }
       main();
    }

    @Override
    protected void onDestroy() {
        isDestroy = true;
        super.onDestroy();
    }

    protected abstract void initContentView(Bundle bundle);

    protected abstract void initData() ;

    protected abstract void initView();

    protected abstract void initTitle() ;

    protected abstract void main();

    protected void showLoadingDialog() {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        if (waitDialog.getAlerType() != SweetAlertDialog.PROGRESS_TYPE) {
            waitDialog.changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
        }
        waitDialog.setTitleText("加载中...");
        waitDialog.setContentText("");
        waitDialog.show();
    }

    protected void showErrorDialog() {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        waitDialog.setTitleText("出错了")
                .setContentText(getString(R.string.please_check_your_network_connection))
                .setConfirmText("确定")
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        waitDialog.show();
    }

    protected void showErrorDialog(String content) {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        waitDialog.setTitleText("出错了")
                .setContentText(content)
                .setConfirmText("确定")
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        waitDialog.show();
    }

    protected void showErrorDialog(int resId) {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        waitDialog.setTitleText("出错了")
                .setContentText(getString(resId))
                .setConfirmText("确定")
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        waitDialog.show();
    }

    protected void hideLoadingDialog() {
        if (waitDialog != null) {
            waitDialog.dismiss();
        }
    }

    protected void showDefaultBackIcon() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    protected void showBackIcon(int resId, View.OnClickListener listener) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(resId);
        if (listener == null) {
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }else {
            mToolbar.setNavigationOnClickListener(listener);
        }
    }

    protected void setTvCenterTitle(String title) {
        mTvCenterTitle.setText(title);
    }

    protected void setTvCenterTitle(int resId) {
        mTvCenterTitle.setText(resId);
    }

    protected void setTvCenterTitleColor(int resId) {
        mTvCenterTitle.setTextColor(context.getResources().getColor(resId));
    }

    protected void Toast(String text) {
        ToastUtils.showToast(text, this);
    }

    protected void Toast(int textId) {
        ToastUtils.showToast(textId, this);
    }


    /**
     * 全透状态栏
     */
    protected void setStatusBarFullTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * 半透明状态栏
     */
    protected void setHalfTransparent() {

        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * 如果需要内容紧贴着StatusBar
     * 应该在对应的xml布局文件中，设置根布局fitsSystemWindows=true。
     */
    private View contentViewGroup;

    protected void setFitSystemWindow(boolean fitSystemWindow) {
        if (contentViewGroup == null) {
            contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        }
        contentViewGroup.setFitsSystemWindows(fitSystemWindow);
    }

    /**
     * 为了兼容4.4的抽屉布局->透明状态栏
     */
    protected void setDrawerLayoutFitSystemWindow() {
        if (Build.VERSION.SDK_INT == 19) {//19表示4.4
            int statusBarHeight = getStatusHeight(this);
            if (contentViewGroup == null) {
                contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            }
            if (contentViewGroup instanceof DrawerLayout) {
                DrawerLayout drawerLayout = (DrawerLayout) contentViewGroup;
                drawerLayout.setClipToPadding(true);
                drawerLayout.setFitsSystemWindows(false);
                for (int i = 0; i < drawerLayout.getChildCount(); i++) {
                    View child = drawerLayout.getChildAt(i);
                    child.setFitsSystemWindows(false);
                    child.setPadding(0,statusBarHeight, 0, 0);
                }

            }
        }
    }

    private static int statusHeight;

    /**
     * 获得状态栏的高度
     *
     * @param context
     */
    public static int getStatusHeight(Context context) {
        if (statusHeight <= 0) {
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
                statusHeight = context.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusHeight;
    }
}
